package com.syb.controller.function;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.syb.entity.function.SysRole;
import com.syb.service.RoleService;
import com.syb.util.PageResults;

@SuppressWarnings("serial")
@Namespace("/role")
public class RoleController extends ActionSupport{
	
	private static  Logger logger  = Logger.getLogger(RoleController.class);
	
	@Autowired
	private RoleService roleService;
	
	private Integer currentPage;
	
	private String name;
	
	private Integer id;
	//资源的id
	private String ids;
	
	private String roleName;
	
	private Integer status;

	@Action(value="getRoleList",
			results={
			@Result(name="success",location="/WEB-INF/page/role/list.jsp"),
			@Result(name="error",location="/WEB-INF/fail.jsp")
			})
	public String  getSysRoleList(){
		PageResults<SysRole>  page = new PageResults<SysRole>();
		//组装参数
		if(name!=null&&name.length()>0){
			page.getParamMap().put("name", name);
		}
		if(currentPage!=null&&!currentPage.equals("")){
			page.setCurrentPage(currentPage);
		}
		PageResults<SysRole> sysRoleList = new PageResults<SysRole>();
		try{
			sysRoleList=roleService.findSysRoleList(page);
		}catch(Exception e){
			logger.error(e.getMessage());
			ActionContext.getContext().put("error", e.getMessage());
		}
		ActionContext.getContext().put("result", sysRoleList);
		return "success";
	}
	
	//获取角色拥有的资源权限
	@Action(value="getResourcesList",
			results={
			@Result(name="success",location="/WEB-INF/page/role/resourcesList.jsp"),
			@Result(name="error",location="/WEB-INF/fail.jsp")
			})
	public String getResources(){
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		try {
			list = roleService.findSysRoleResources(id);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return "error";
		}
		ActionContext.getContext().put("result", list);
		ActionContext.getContext().put("id", id);
		return "success";
	} 
	
	//更新角色所拥有的资源
	@Action(value="updateResourcesList",
			results={
			@Result(name="success",type="redirect",location="/role/getRoleList"),
			@Result(name="error",location="/WEB-INF/fail.jsp")
			})
	public String updateResourcesList(){
		 List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		 Map<String,Object> map = null;
		if(ids.length()>0){
		   ids = ids.substring(0, ids.length()-1);
		  String idss[] = ids.split(",");
		  for(int i=0;i<idss.length;i++){
			  map = new HashMap<String,Object>();
			  map.put("resourceId", idss[i]);
			  map.put("id", id);
			  list.add(map);
		  }
		}else{
			map = new HashMap<String,Object>();
			map.put("id", id);
			list.add(map);
		}
		try{
			roleService.updateRoleResourcesList(list);
		}catch(Exception e){
			logger.error(e.getMessage());
			return "error";
		}
		
		return "success";
	}

	/**
	 * 更新跳转页面
	 * @return
	 */
	@Action(value="findRoleById",
			results={
			@Result(name="success",location="/WEB-INF/page/role/update.jsp"),
			@Result(name="error",location="/WEB-INF/fail.jsp")
			})
	public String findRoleById(){
		SysRole sys = new SysRole();
		try{
			sys = roleService.findSysRoleById(id);
		}catch(Exception e){
			logger.error(e.getMessage());
			return "error";
		}
		ActionContext.getContext().put("result", sys);
		return "success";
	}
	
	
	/**
	 * 更新角色
	 * @return
	 */
	@Action(value="updateRole",
			results={
			@Result(name="success",type="redirect",location="/role/getRoleList"),
			@Result(name="error",location="/WEB-INF/page/role/update.jsp")
			})
	public String updateRole(){
		SysRole sys = new SysRole();
		sys.setId(id);
		sys.setRoleName(roleName);
		sys.setStatus(status);
		try{
			roleService.insertRole(sys);
		}catch(Exception e){
			ActionContext.getContext().put("result", sys);
			ActionContext.getContext().put("error",e.getMessage());
			return "error";
		}
		ActionContext.getContext().put("result", sys);
		return "success";
	}
	
	
	/**
	 * 添加角色跳转页面
	 * @return
	 */
	@Action(value="addRole",
			results={
			@Result(name="success",location="/WEB-INF/page/role/add.jsp"),
			@Result(name="error",location="/WEB-INF/fail.jsp")
			})
	public String addRole(){
		
		return "success";
	}
	
	
	/**
	 * 添加角色跳转页面
	 * @return
	 */
	@Action(value="insertRole",
			results={
			@Result(name="success",type="redirect",location="/role/getRoleList"),
			@Result(name="error",location="/WEB-INF/page/role/update.jsp")
			})
	public String insertRole(){
		SysRole sys = new SysRole();
		sys.setRoleName(roleName);
		sys.setStatus(status);
		try{
			roleService.insertRole(sys);
		}catch(Exception e){
			ActionContext.getContext().put("result", sys);
			ActionContext.getContext().put("error",e.getMessage());
			return "error";
		}
		return "success";
	}
	
	/**
	 * 添加角色跳转页面
	 * @return
	 */
	@Action(value="deleteRole",
			results={
			@Result(name="success",type="redirect",location="/role/getRoleList"),
			@Result(name="error",location="/WEB-INF/fail.jsp")
			})
	public String deleteRole(){
		try{
			roleService.deleteRole(id);
		}catch(Exception e){
			ActionContext.getContext().put("error",e.getMessage());
			return "error";
		}
		return "success";
	}
	
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}


	public int getCurrentPage() {
		return currentPage;
	}


	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public String getRoleName() {
		return roleName;
	}

	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}
	
}
